#include "Helper.h"

using namespace ecocity;

//---------------------------------------------------------------

QStringList
Helper::get_product_names( const std::vector< Product >& products )
{
    QStringList list;

    for ( uint i = 0; i < products.size(); ++i )
    {
        list << products[ i ].name( );
    }

    return list;
}

//---------------------------------------------------------------

QStringList
Helper::get_product_names( const std::vector< InvoiceItem >& items )
{
    QStringList list;

    for ( uint i = 0; i < items.size(); ++i )
    {
        // i.e. 1 x 1/2 dozena ous
        list << QString( "%1 x %2" ).arg( QString::number( items[ i ].quantity( ) ),
                                          items[ i ].product( ).name( ) );
    }

    return list;
}

//---------------------------------------------------------------

QStringList
Helper::get_proveider_names( const std::vector< Product >& products )
{
    QStringList list;

    for ( uint i = 0; i < products.size(); ++i )
    {
        const QString& name = products[ i ].proveider( ).name( );
        if ( !list.contains( name, Qt::CaseInsensitive ) )
        {
            list << name;
        }
    }

    return list;
}

//---------------------------------------------------------------

QList<int>
Helper::get_tax_rates( const Invoice& invoice )
{
    QList<int> tax_rates_list;

    std::vector< InvoiceItem > items = invoice.items( );

    for ( size_t i = 0; i < items.size(); ++i )
    {
        int tax_rate = items.at( i ).tax_rate( );
        if ( !tax_rates_list.contains( tax_rate ) )
        {
            tax_rates_list << tax_rate;
        }
    }

    return tax_rates_list;
}

//---------------------------------------------------------------

float
Helper::get_taxes( const std::vector<InvoiceItem> &items, int tax_rate )
{
    float taxes = 0;

    for ( size_t i = 0; i < items.size(); ++i )
    {
        int rate = items.at( i ).tax_rate( );
        if ( tax_rate == rate )
        {
            taxes += items.at( i ).taxes_cost( );
        }
    }

    return taxes;
}

//---------------------------------------------------------------

QMap< int, float >
Helper::get_taxes( const Invoice &invoice, QList<int> tax_rates )
{
    QMap< int, float > taxes_map;

    QList<int>::const_iterator iterator_tax_rates;

    for ( iterator_tax_rates = tax_rates.constBegin();
          iterator_tax_rates < tax_rates.constEnd( );
          ++iterator_tax_rates )
    {
        int tax_rate = *iterator_tax_rates;

        float taxes = get_taxes( invoice.items( ), tax_rate );

        taxes_map[ tax_rate ] = taxes;
    }

    return taxes_map;
}

//---------------------------------------------------------------

std::vector< Proveider >
Helper::get_proveiders( const std::vector< Product >& products )
{
    QStringList list_names = Helper::get_proveider_names( products );
    std::vector< Proveider > proveiders;

    foreach ( QString name, list_names )
    {
        proveiders.push_back( Proveider( name ) );
    }

    return proveiders;
}

//---------------------------------------------------------------
